package cn.hx.controller.front.item;

import cn.hx.entity.*;
import cn.hx.service.ItemInfoService;
import cn.hx.service.ItemUserService;
import cn.hx.service.UserInfoService;
import cn.hx.service.UserReportService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;


@Controller
public class ItemUserReportController {
    /**
     * 服务对象
     */
    @Resource
    private UserReportService userReportService;
    @Resource
    private ItemInfoService itemInfoService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private ItemUserService itemUserService;


    @GetMapping("re_approve1/getDeclare")
    public ModelAndView getDeclare(Integer id, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("re_approve/addDeclare");
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        UserInfo userInfo1 = userInfoService.queryById(userInfo.getId());
        ItemInfo itemInfo = itemInfoService.queryById(id);
        System.out.println(itemInfo);
        modelAndView.addObject("itemInfo", itemInfo);
        modelAndView.addObject("userInfo", userInfo1);
        List<ItemUser> itemUsers = itemUserService.queryById1(id);
        modelAndView.addObject("itemUser",itemUsers);
        return modelAndView;
    }

    @Resource
    private JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String username;
    
    @PostMapping("re_approve/addDeclare")
    @ResponseBody
    public String addDeclare(UserReport userReport, HttpSession session,Integer itemId) {
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        userReport.setUserId(userInfo.getId());
        Item item = new Item();
        item.setId(itemId);
        userReport.setItem(item);
        Date date = new Date();
        userReport.setCreateTime(date);
        userReport.setStatus(2);
        int i = userReportService.insert(userReport);
        if (i > 0) {

//            UserInfo userInfo1 = userInfoService.queryById(userReport.getItemUserid());
//            String email = userInfo1.getEmail();
//
//            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//            simpleMailMessage.setFrom(username);
//            simpleMailMessage.setTo(email);
//            simpleMailMessage.setSubject("您有新的审批");
//            simpleMailMessage.setText(userReport.toString());
//            mailSender.send(simpleMailMessage);

            return "1";
        } else {
            return "0";
        }
    }
    @Value("${web.upload-path}")
    private String path;

    @PostMapping("fileUp")
    @ResponseBody
    public String fileUp(MultipartFile file) throws IOException {
        if (file != null && !file.isEmpty()) {
//            String path = new File("").getAbsolutePath();
            String filename = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String type = filename.substring(filename.lastIndexOf("."));
            String s = null;
            File file1 = new File(path + "/.png");
            if (!file1.exists()) {
                file1.mkdirs();
            }

            file.transferTo(new File(file1, uuid + type));
                s = "/.png/" + uuid + type;
                return s;

        }
        return "0";
    }
}